package br.ufmg.ubicomp.droidguide.eventservice;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * class responsible for firing periodic events (client to server and 
 * request data from server)
 * @author wpjr
 *
 */
public class DroidGuideTimerTask {
    Timer timer;
    private byte numSecondsPerTimer;
    private boolean stop;
    private int numWarningBeeps;

    public DroidGuideTimerTask(byte numSecondsPerTimer) {
    	this.numSecondsPerTimer = numSecondsPerTimer;
        timer = new Timer();
        timer.schedule(new RemindTask(),
                       0,        //initial delay
                       this.numSecondsPerTimer*1000);  //subsequent rate
    }

    class RemindTask extends TimerTask {
        //int numWarningBeeps = 3;
        public void run() {
            if (!stop) {
                System.out.println(getStringDateTime() + " - Timer");
                numWarningBeeps--;
            } else {
                System.out.println(getStringDateTime() + " - Time's up!");
                //timer.cancel(); // Not necessary because
                                  // we call System.exit
                System.exit(0);   // Stops the AWT thread 
                                  // (and everything else)
            }
        }
		private String getStringDateTime() {
			Calendar dateTime = Calendar.getInstance();
			return new SimpleDateFormat("yyyy/MM/dd hh:mm:ssss").format(dateTime.getTime());
		}
    }
    
	public boolean isStop() {
		return stop;
	}

	public void setStop(boolean stop) {
		this.stop = stop;
	}   
    
}